<?php


namespace app\admin\controller;

use app\common\bean\ListMap;
use app\common\bean\UserBean;
use app\common\bean\UserRechargeBean;
use app\common\bean\UserVipBean;
use app\common\controller\AdminController;
use app\common\enum\UserStatusEnum;
use app\common\manage\UserManage;
use app\common\model\UserVipModel;
use app\common\utils\ExitJsonUtil;
use think\Exception;

class User extends AdminController
{
    /** 用户列表
     * @return mixed
     * @throws \Exception
     */
    public function userList(){
        $listMap = new ListMap();
        $listMap->setData($this->getParams);
        $userMap = UserManage::getInstance()->getPaginatorList($listMap);
        $this->assign("userList",$userMap->getList());
        $this->assign("currentPage",$userMap->getCurrentPage());
        $this->assign("totalCount",$userMap->getTotalCount());
        $this->assign("statusSelect",UserStatusEnum::getArrayName());
        return $this->fetch();
    }

    /**
     * 修改用户
     * @throws Exception
     */
    public function editUser(){
        $userBean = new UserBean();
        if(request()->isPost()){
            $preg = '/^([12]\d\d\d)-(0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[0-1])$/';
            if(preg_match($preg, $this->postParams['endTime']) && preg_match($preg, $this->postParams['beginTime'])){
                $uid = $this->postParams['id'];
                $vdata[UserVipBean::$vipStartTime] = $this->postParams['beginTime'];
                $vdata[UserVipBean::$vipEndTime] = $this->postParams['endTime'];
                if( $vip = UserVipModel::getInstance()->where([UserVipBean::$userId=>$this->postParams['id']])->find() ){
                    UserVipModel::getInstance()->where([UserVipBean::$userId=>$uid])->update( $vdata );
                }else{
                    $vdata[UserVipBean::$userId] = $uid;
                    $vdata[UserVipBean::$vipPrice] = 1;
                    $vdata[UserVipBean::$payPrice] = 1;
                    $vdata[UserVipBean::$createdAt] = date('Y-m-d H:i:s');
                    UserVipModel::getInstance()->insert( $vdata );
                }
            }
            unset($this->postParams['beginTime']);
            unset($this->postParams['endTime']);
            $userBean->setData($this->postParams);
            $result = UserManage::getInstance()->edit($userBean);
            ExitJsonUtil::getInstance()->exitData($result);
        }
        $userBean->setData($this->getParams);
        $userBean = UserManage::getInstance()->getInfo($userBean);
        $this->assign('user',$userBean->getData());
        $this->assign("statusSelect",UserStatusEnum::getArrayName());
        return $this->fetch();
    }
}